﻿<%@ WebHandler Language="C#" Class="Audit" %>

using System;
using System.Web;
using System.Collections.Generic;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Course;
using Company.StudyClub.BLL.Common;
using System.Text;
using System.IO;
using System.Threading;

public class Audit : IHttpHandler
{    
    private HttpHelper helper = new HttpHelper();
    
    public void ProcessRequest (HttpContext context) {
        HttpResponse response = context.Response;

        switch (helper.GetString("type"))
        {
            case "coursepassed":
                AuditCourse(response);
                break;
            
            default: break;
        }
    }

    private void AuditCourse(HttpResponse response)
    {
        CourseBLL objBLL = new CourseBLL();

        try
        {
            string[] cids = helper.GetString("cids").Split(',');

            for (int i = 0; i < cids.Length; i++)
            {
                if (cids[i] == string.Empty)
                    continue;

               
                objBLL.UpdateStatus(int.Parse(cids[i]),1,helper.CurrentUser.UserID);
            }
            
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }           
    }

    private void CreateCourse(HttpResponse response)
    {
        Course_Info model = new Course_Info();
        model.CourseName = helper.GetString("cname");
        model.CourseType = helper.GetInt("ctype");
        model.PlayLength = helper.GetInt("playlength") * 60;
        model.IsPublic = helper.GetInt("ispublic");
        model.IsRecommend = helper.GetInt("isrecommend");
        model.Keywords = helper.GetString("keywords");
        model.LecturerIntro = helper.GetString("intro");
        model.Description = helper.GetString("description");
        model.SerialNumber = helper.GetString("serialnumber");
        model.Status = 0;
        model.CatalogID = helper.GetInt("catalogid");
        model.CoverIcon = helper.GetString("covericon");  

        CourseBLL objBLL = new CourseBLL();
        int courseId = objBLL.Add(model);

        CatalogBLL objCatalog = new CatalogBLL();
        objCatalog.UpdateTotalCourses(model.CatalogID, 1);
        
        response.Write(courseId.ToString());
    }

    private void UpdateCourse(HttpResponse response)
    {
        Course_Info model = new Course_Info();
        model.CourseID = helper.GetInt("courseid");
        model.CourseName = helper.GetString("cname");
        model.PlayLength = helper.GetInt("playlength") * 60;
        model.IsPublic = helper.GetInt("ispublic");
        model.IsRecommend = helper.GetInt("isrecommend");
        model.Keywords = helper.GetString("keywords");
        model.LecturerIntro = helper.GetString("intro");
        model.Description = helper.GetString("description");        
        model.CatalogID = helper.GetInt("catalogid");
        model.CoverIcon = helper.GetString("covericon");
        
        model.Status = -1;
        model.SerialNumber = null;
        model.CreatorID = -1;
        model.CreateTime = DateTime.Parse("1900-1-1");
        model.CourseType = -1;
        model.SchoolID = -1;
        
        CourseBLL objBLL = new CourseBLL();
        objBLL.Update(model);

        int curcatalogid = helper.GetInt("curcatalogid");

        if (curcatalogid != model.CatalogID)
        {
            CatalogBLL objCatalog = new CatalogBLL();
            objCatalog.UpdateTotalCourses(model.CatalogID, 1);
            objCatalog.UpdateTotalCourses(curcatalogid, -1);
        }

        response.Write("1");
    }

    private void UploadScorm(HttpContext context)
    {
        HttpPostedFile oFile = context.Request.Files["Filedata"];
       
        int courseId = helper.GetInt("cid");

        string m_path = HttpContext.Current.Server.MapPath(@context.Request["folder"]);
        
        DeleteOldFile(m_path);
                
        Directory.CreateDirectory(m_path);

        SectionBLL objSection = new SectionBLL();
        objSection.DeleteByCourseID(courseId);

        string m_filepath = Path.Combine(m_path, oFile.FileName);
        oFile.SaveAs(m_filepath);

        context.Response.Write("1");
    }

    private void DoImportSection(HttpContext context)
    {
        string filename = context.Server.MapPath(helper.GetString("filePath"));
        string directory = Path.GetDirectoryName(filename);
        int courseId = helper.GetInt("cid");
        
        UnZipFile(directory, filename);

        ImportSections(courseId, directory);

        context.Response.Write("1");
    }

    private void DeleteOldFile(string folderPath)
    {
        if (Directory.Exists(folderPath))
            Directory.Delete(folderPath, true);
    }

    private void DeleteOldSections(int CourseID)
    {
        SectionBLL objBLL = new SectionBLL();
        objBLL.Delete(CourseID);
    }

    protected void UnZipFile(string filepath, string unZipFolder)
    {
        try
        {
            SuperZip.Instance().UnZipFile(filepath, unZipFolder);

            Thread.Sleep(3000);

            FileInfo objFile = new FileInfo(filepath);
            if (objFile.Exists)
            {
                objFile.Delete();
            }
        }
        catch
        {
            return;
        }
    }

    protected void ImportSections(int CourseID,string folderPath)
    {
        string ls_ErrText = "";
        string xmlFilePath = folderPath + "imsmanifest.xml";
        if (File.Exists(xmlFilePath))
        {
            XmlHelper xmlHelper = new XmlHelper(xmlFilePath);
            xmlHelper.InitSectionList(CourseID, ref ls_ErrText);
            List<Course_Section> lstSection = xmlHelper.SectionList;
            if (ls_ErrText.Trim() == "")
            {
                SectionBLL objBLL = new SectionBLL();

                foreach (Course_Section model in lstSection)
                {
                    objBLL.Add(model);
                }
            }
            else
            {
                if (!Directory.Exists(folderPath))
                    Directory.Delete(folderPath, true);

                return;
            }
        }
        else
        {
            if (!Directory.Exists(folderPath))
                Directory.Delete(folderPath, true);

            return;
        }
    }

    private void UploadCoverIcon(HttpContext context)
    {
        HttpPostedFile oFile = context.Request.Files["Filedata"];

        string directory = HttpContext.Current.Server.MapPath(@context.Request["folder"]);
        string filename = helper.GetString("covericon");
        
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);

        string m_filepath = Path.Combine(directory, filename.Replace("_S","_O"));
        oFile.SaveAs(m_filepath);

        CutImage objImage = new CutImage();
        objImage.CutImageBySize(m_filepath, m_filepath.Replace("_O", "_S"), 160, 120);

        int courseId = helper.GetInt("courseid");
        if (courseId > 0)
        {
            CourseBLL objBLL = new CourseBLL();
            objBLL.UpdateCoverInfo(courseId, filename);
        }
        
        context.Response.Write("1");
    }

    private void DeleteCoverIcon(HttpContext context)
    {
        string filepath = context.Server.MapPath("~/data/coursepicture/" + helper.GetString("filename"));

        if (File.Exists(filepath))
        {
            File.Delete(filepath);
        }
        
        string oPicture = filepath.Replace("_S", "_O");
        if (File.Exists(oPicture))
        {
            File.Delete(oPicture);
        }

        int courseId = helper.GetInt("courseid");
        if (courseId > 0)
        {
            CourseBLL objBLL = new CourseBLL();
            objBLL.UpdateCoverInfo(courseId, "");
        }
        context.Response.Write("1");
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}